{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "private_outputs": true,
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# Pinecone Assistant Getting Started\n",
        "\n",
        "Welcome to the getting started Notebook for [Pinecone assistant](https://www.pinecone.io/blog/pinecone-assistant/)!"
      ],
      "metadata": {
        "id": "IpHsLVa0mAJe"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Install pinecone client and assistants plugin\n",
        "!pip install --upgrade pinecone-client pinecone-plugin-assistant"
      ],
      "metadata": {
        "id": "bAEbPIcn46t2"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Install Pinecone notebook utilities including the Pinecone Connect widget\n",
        "!pip install pinecone-notebooks==0.1.1"
      ],
      "metadata": {
        "id": "v6cdZP0ylfC5"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import os\n",
        "\n",
        "if not os.environ.get(\"PINECONE_API_KEY\"):\n",
        "    from pinecone_notebooks.colab import Authenticate\n",
        "    Authenticate()"
      ],
      "metadata": {
        "id": "HO8ass1Qmdgq"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [],
      "metadata": {
        "id": "lixB5GGnmAHv"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from google.colab import userdata\n",
        "from pinecone import Pinecone\n",
        "\n",
        "pc = Pinecone(api_key=os.environ.get('PINECONE_API_KEY') or userdata.get('PINECONE_API_KEY'))\n",
        "\n",
        "assistant_name = 'HelloPineconeAssistant'\n",
        "\n",
        "metadata = {\"author\": \"Jane Doe\", \"version\": \"1.0\"}\n",
        "\n",
        "assistant = pc.assistant.create_assistant(\n",
        "    assistant_name=assistant_name,\n",
        "    metadata=metadata,\n",
        "    timeout=30 # Wait 30 seconds for assistant creation to complete.\n",
        ")"
      ],
      "metadata": {
        "id": "unDcuyR2EaJr"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Get assistant status\n",
        "assistant_status = pc.assistant.describe_assistant(assistant_name=assistant_name)\n",
        "assistant_status"
      ],
      "metadata": {
        "id": "HJ5fxLvY97yF"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Upload the toys.txt file from the repository to Google Colab:\n",
        "\n",
        "1. Click the folder icon in Google Colab's left rail <--\n",
        "2. Click the upload button and use the file picker to select `toys.txt` from the subdirectory of https://github.com/pinecone-io/examples that contains this notebook\n",
        "3. Run the next cell to upload the toys.txt file to your Pinecone assistant\n"
      ],
      "metadata": {
        "id": "nIyz6gYqAbHn"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import os\n",
        "\n",
        "# Target our existing Pinecone Assistant\n",
        "assistant = pc.assistant.Assistant(\n",
        "    assistant_name=assistant_name,\n",
        ")\n",
        "\n",
        "file_path = '/content/toys.txt'\n",
        "\n",
        "# Upload the file\n",
        "if os.path.exists(file_path):\n",
        "    response = assistant.upload_file(\n",
        "        file_path=file_path,\n",
        "        timeout=None\n",
        "    )\n",
        "    print(f\"Uploaded {file_path}\")\n",
        "    print(\"Response:\", response)\n",
        "else:\n",
        "    print(f\"File not found: {file_path}\")"
      ],
      "metadata": {
        "id": "CVixRBQfEKpo"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# List uploaded files our assistant is aware of\n",
        "files = assistant.list_files()\n",
        "files"
      ],
      "metadata": {
        "id": "fqYUFoZ8N487"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Chat with your Pinecone assistant, which automatically references\n",
        "# your uploaded documents in its responses\n",
        "from pinecone_plugins.assistant.models.chat import Message\n",
        "chat_context = [Message(content='Which toys teach STEM skills?')]\n",
        "response = assistant.chat_completions(messages=chat_context)\n",
        "print(response)"
      ],
      "metadata": {
        "id": "iKLZtk7BOomq"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "GQKf1l3rOFhy"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}
